package com.liuqing.www.java基础知识;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class 反射 {
	public static void main(String[] args) {
		try{
//				System.out.println(Student.class.getInterfaces()[0].getName() );
				Method method=Student.class.getMethod("sayYourName");//private方法得不到
				method.invoke(Student.class.newInstance());
				Student fore=Student.class.newInstance();
//				Constructor<Student> c=Student.class.getConstructor(String.class,Integer.class);
//				fore=c.newInstance("zzzz",100);
				for(Constructor co:Student.class.getConstructors()){
					if(co.getParameterTypes().length>0){
						fore=(Student)co.newInstance("zzzz",100);
					}
				}
				
				Field f=	Student.class.getDeclaredField("name");
				f.setAccessible(true);
				System.out.println(""+f.get(fore));
				f.set(fore,"~~~~~");
				System.out.println(""+f.get(fore));
				Method m=Student.class.getDeclaredMethod("sayYourName", String.class);
				m.setAccessible(true);
				m.invoke(fore, "xxx");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
class Student{
	private String name;
	private int age;
	public Student(){
		
	}
	public Student(String name,int age){
		this.name=name;
		this.age=age;
	}
	private void sayYourName(String name){
		System.out.println(name+"在睡觉.....");
	}
	public void sayYourName(){
		System.out.println(name+"在睡觉.....");
	}
}
